package xavier

import (
	"net/http"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

type DefaultController struct{}

func (d *DefaultController) Index(c *gin.Context) {
	//设置session
	session := sessions.Default(c)
	session.Set("username", "xavier")
	session.Save()
	c.HTML(http.StatusOK, "default/index.html", gin.H{
		"title": "首页",
	})
}
func (d *DefaultController) News(c *gin.Context) {
	//获取session
	session := sessions.Default(c)
	username := session.Get("username")
	c.String(http.StatusOK, "username = %v", username)
}

// ArticleInput 用于接收表单或 JSON 的参数
type ArticleInput struct {
    Title   string `form:"title" json:"title" binding:"required"`
    Content string `form:"content" json:"content" binding:"required"`
}

// CreateArticle 处理 POST 提交，支持 JSON 和表单
func (d *DefaultController) CreateArticle(c *gin.Context) {
    var input ArticleInput
    // ShouldBind 会根据 Content-Type 自动选择 JSON 或表单绑定
    if err := c.ShouldBind(&input); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{
            "error":   "参数错误",
            "details": err.Error(),
        })
        return
    }

    // 这里可以执行保存逻辑（数据库等），示例直接回显
    c.JSON(http.StatusOK, gin.H{
        "message": "创建成功",
        "data": gin.H{
            "title":   input.Title,
            "content": input.Content,
        },
    })
}
